package com.game.spacetrader;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Base adapter used for listView for Universe View Page
 *
 */
public class UniverseBaseAdapter extends BaseAdapter {
	private static ArrayList<Planet> searchArrayList;

	private LayoutInflater mInflater;

	/**
	 * Constructor for the class
	 * @param context the context that the view should be in
	 * @param results the ArrayList of Planets that will be in the listview
	 */
	public UniverseBaseAdapter(Context context, ArrayList<Planet> results) {
		searchArrayList = results;
		mInflater = LayoutInflater.from(context);
	}

	/**
	 * Getter
	 * @return the number of items in the listView
	 */
	public int getCount() {
		return searchArrayList.size();
	}

	/**
	 * Getter
	 * @param position the position of the wanted item
	 * @return an item at a given position in the ArrayList
	 */
	public Object getItem(int position) {
		return searchArrayList.get(position);
	}

	/**
	 * Getter
	 * @param position the position of the wanted item
	 * @return the id of the item at the inputted position
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Gets the View of the item at the given position in the ArrayList, and creates it
	 * @param position the position of the wanted item
	 * @param convertView the view to be created
	 * @param parent the group that the View will be a part of
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.universe_row_view, null);
			holder = new ViewHolder();
			holder.txtName = (TextView) convertView.findViewById(R.id.name);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.txtName.setText(searchArrayList.get(position).getName());
		return convertView;
	}

	/**
	 * Class to hold all of the information of the Incident to be included in the listView
	 *
	 */
	static class ViewHolder {
		TextView txtName;
	}
}